package com.dragonboat.controller;
import com.dragonboat.entity.Audience;
import com.dragonboat.entity.Audience;
import com.dragonboat.repository.AudienceRepository;
import com.dragonboat.entity.Audience;
import com.dragonboat.entity.Event;
import com.dragonboat.service.AudienceService;
import com.dragonboat.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.dragonboat.util.PasswordUtil;
@RestController
@RequestMapping("/audience")
public class AudienceController {
    @Autowired
    private AudienceService audienceService;
    @Autowired
    private AudienceRepository audienceRepository;

    @PostMapping("/register")
    public ResponseResult<Audience> register(@RequestBody Map<String, String> request) {
        String audienceName = request.get("audienceName");
        String password = request.get("password");

        if (audienceRepository.existsByAudienceName(audienceName)) {
            return ResponseResult.error("用户名称已存在");
        }

        Audience audience = new Audience();
        audience.setAudienceName(audienceName);
        audience.setPassword(PasswordUtil.encode(password)); // 使用哈希密码

        audienceRepository.save(audience);
        audience.setPassword(null); // 不返回密码

        return ResponseResult.success(audience);
    }

    @PostMapping("/login")
    public ResponseResult<Audience> login(@RequestParam Integer audienceId,
                                          @RequestParam String password) {
        return audienceService.login(audienceId, password)
                .map(audience -> {
                    audience.setPassword(null); // 不返回密码
                    return ResponseResult.success(audience);
                })
                .orElse(ResponseResult.error("Login failed"));
    }

    @GetMapping("/findById/{audienceId}")
    public ResponseResult<Audience> findById(@PathVariable Integer audienceId) {
        return audienceService.findById(audienceId)
                .map(audience -> {
                    audience.setPassword(null); // 不返回密码
                    return ResponseResult.success(audience);
                })
                .orElse(ResponseResult.error("Audience not found"));
    }
    @PostMapping("/loginByname")
    public ResponseResult<Audience> loginByName(@RequestParam String audienceName,
                                                @RequestParam String password) {
        return audienceRepository.findByAudienceName(audienceName)
                .filter(a -> PasswordUtil.matches(password, a.getPassword()))  // 使用密码工具验证
                .map(audience -> {
                    audience.setPassword(null);
                    return ResponseResult.success(audience);
                })
                .orElse(ResponseResult.error("Login failed"));
    }
    @GetMapping("/events")
    public ResponseResult<List<Event>> getUpcomingEvents() {
        return ResponseResult.success(audienceService.getUpcomingEvents());
    }

    @GetMapping("/event/{eventId}")
    public ResponseResult<Event> getEventDetails(@PathVariable Integer eventId) {
        return audienceService.getEventDetails(eventId)
                .map(ResponseResult::success)
                .orElse(ResponseResult.error("Event not found"));
    }
    @PostMapping("/updateInfo")
    public ResponseResult<Audience> updateInfo(@RequestParam Integer audienceId,
                                               @RequestParam(required = false) String name,
                                               @RequestParam(required = false) String oldPassword,
                                               @RequestParam(required = false) String newPassword) {
        // 验证旧密码
        if (newPassword != null && !newPassword.isEmpty()) {
            Audience audience = audienceService.getAudienceById(audienceId);
            if (audience == null || !PasswordUtil.matches(oldPassword, audience.getPassword())) {
                return ResponseResult.error("原密码不正确");
            }
            // 加密新密码
            newPassword = PasswordUtil.encode(newPassword);
        }
        return ResponseResult.success(audienceService.updateAudienceInfo(audienceId, name, newPassword));
    }

}